package com.idealighter.game.games.doudizhu.manager;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.dictionary.dic.DoudizhuMatchRoomDic;
import com.idealighter.game.dictionary.domwrapper.DoudizhuMatchRoomDomWrapper;
import com.idealighter.game.games.common.AbstractTable;
import com.idealighter.game.games.doudizhu.struct.DouDiZhuAiSeat;
import com.idealighter.game.games.doudizhu.struct.DouDiZhuRoom;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 斗地主数据管理 .
 * 
 * @date 2015年8月6日 下午9:39:01
 *
 */
@Singleton
public class DouDiZhuDataMgr {

  private static final Logger LOG = LoggerFactory.getLogger(DouDiZhuDataMgr.class);

  private DoudizhuMatchRoomDic matchRoomDic;

  // 斗地主游戏房间
  private final Map<Integer, DouDiZhuRoom> rooms;
  // 玩家座位
  private final Map<Long, DouDiZhuAiSeat> playerSeats = new HashMap<>();

  @Inject
  public DouDiZhuDataMgr(DoudizhuMatchRoomDic matchRoomDic) {
    this.matchRoomDic = matchRoomDic;
    this.rooms = createRooms();
  }

  /**
   * 创建房间 .
   * 
   * @return
   */
  private Map<Integer, DouDiZhuRoom> createRooms() {
    Map<Integer, DouDiZhuRoom> rooms = new HashMap<>();
    for (DoudizhuMatchRoomDomWrapper roomDom : matchRoomDic.list()) {
      rooms.put(roomDom.getId(), new DouDiZhuRoom(roomDom.getId(), roomDom.getName()));
      LOG.info("[斗地主]创建房间[{}][{}]", roomDom.getId(), roomDom.getName());
    }

    return rooms;
  }

  /**
   * 获取房间 .
   * 
   * @return
   */
  public Map<Integer, DouDiZhuRoom> getRooms() {

    return rooms;
  }

  /**
   * 获取房间 .
   * 
   * @param roomId .
   * @return
   */
  public DouDiZhuRoom getRoom(int roomId) {

    return rooms.get(roomId);
  }

  /**
   * 获取玩家的座位 .
   * 
   * @param playerId 玩家id.
   * @return
   */
  public DouDiZhuAiSeat getPlayerSeat(long playerId) {

    return playerSeats.get(playerId);
  }

  /**
   * 获取房间id .
   * 
   * @param tableId .
   * @return
   */
  public int getRoomId(int tableId) {

    return tableId / AbstractTable.MAX_TABLE;
  }

  /**
   * 获取所有房间集合 .
   * 
   * @return
   */
  public Collection<DouDiZhuRoom> rooms() {

    return rooms.values();
  }

  /**
   * 更新玩家座位 .
   * 
   * @param playerId 玩家id.
   * @param seat 座位信息.
   */
  public void updatePlayerSeats(long playerId, DouDiZhuAiSeat seat) {
    playerSeats.put(playerId, seat);
  }

  /**
   * 移除玩家座位 .
   * 
   * @param playerId 玩家id.
   */
  public void removePlayerSeat(long playerId) {
    playerSeats.remove(playerId);
  }

}
